# -*- coding: utf-8 -*-

from odoo import models, fields, api,_
from odoo.tools import float_compare, get_lang
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_compare
from datetime import datetime
from odoo.exceptions import UserError, AccessError, ValidationError
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'


    @api.onchange('product_id','partner_id')
    def product_id_change(self):
        if not self.product_id:
            return
        if self.order_id.partner_id.tax_id:
            self.tax_id = self.order_id.partner_id.tax_id
        valid_values = self.product_id.product_tmpl_id.valid_product_template_attribute_line_ids.product_template_value_ids
        # remove the is_custom values that don't belong to this template
        for pacv in self.product_custom_attribute_value_ids:
            if pacv.custom_product_template_attribute_value_id not in valid_values:
                self.product_custom_attribute_value_ids -= pacv

        # remove the no_variant attributes that don't belong to this template
        for ptav in self.product_no_variant_attribute_value_ids:
            if ptav._origin not in valid_values:
                self.product_no_variant_attribute_value_ids -= ptav

        vals = {}
        if not self.product_uom or (self.product_id.uom_id.id != self.product_uom.id):
            vals['product_uom'] = self.product_id.uom_id
            vals['product_uom_qty'] = self.product_uom_qty or 1.0

        product = self.product_id.with_context(
            lang=get_lang(self.env, self.order_id.partner_id.lang).code,
            partner=self.order_id.partner_id,
            quantity=vals.get('product_uom_qty') or self.product_uom_qty,
            date=self.order_id.date_order,
            pricelist=self.order_id.pricelist_id.id,
            uom=self.product_uom.id
        )

        vals.update(name=self.get_sale_order_line_multiline_description_sale(product))

        self._compute_tax_id()

        # if self.order_id.pricelist_id and self.order_id.partner_id:
        #     vals['price_unit'] = self.env['account.tax']._fix_tax_included_price_company(
        #         self._get_display_price(product), product.taxes_id, self.tax_id, self.company_id)
        self.update(vals)

        title = False
        message = False
        result = {}
        warning = {}
        if product.sale_line_warn != 'no-message':
            title = _("Warning for %s") % product.name
            message = product.sale_line_warn_msg
            warning['title'] = title
            warning['message'] = message
            result = {'warning': warning}
            if product.sale_line_warn == 'block':
                self.product_id = False

        return result

    def _compute_tax_id(self):
        for line in self:
            # If company_id is set, always filter taxes by the company
            line.tax_id = self.order_id.partner_id.tax_id




class PurchaseOrderLine(models.Model):
    _inherit = 'purchase.order.line'

    @api.constrains('taxes_id')
    def _check_tax_id(self):
        if self.taxes_id and len(self.taxes_id)>1:
            raise ValidationError(_('税率不能超过1个'))


    # description = fields.Char(string=u'规格')
    #
    # @api.onchange('product_id')
    # def onchange_product_id_for_des(self):
    #     if not self.product_id:
    #         self.description = ''
    #     else:
    #         self.description = self.product_id.description




    @api.onchange('product_id')
    def onchange_product_id(self):
        if not self.product_id:
            return

        # Reset date, price and quantity since _onchange_quantity will provide default values
        self.date_planned = datetime.today().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        self.price_unit = self.product_qty = 0.0
        if self.order_id.partner_id.tax_id:
            self.taxes_id = self.order_id.partner_id.tax_id


        self._product_id_change()

        self._suggest_quantity()
        self._onchange_quantity()

    def _compute_tax_id(self):
        for line in self:

            line.taxes_id = self.order_id.partner_id.tax_id

    @api.onchange('product_qty', 'product_uom')
    def _onchange_quantity(self):
        pass




